const app = getApp()

Page({
  data: {
    tabs: ['进行中', '已成功', '已失败'],
    currentTab: 0,
    bargains: [],
    statusBarHeight: 0,
    navbarHeight: 0
  },

  onLoad() {
    // 获取系统信息，用于适配状态栏高度
    const systemInfo = wx.getSystemInfoSync()
    const statusBarHeight = systemInfo.statusBarHeight || 0
    // 导航栏总高度 = 状态栏高度 + 88rpx (导航栏内容高度，约44px)
    const navbarHeight = statusBarHeight + 44
    this.setData({
      statusBarHeight: statusBarHeight,
      navbarHeight: navbarHeight
    })
    this.loadBargains()
  },

  onShow() {
    this.loadBargains()
  },

  loadBargains() {
    // 从本地存储加载砍价数据
    const bargains = wx.getStorageSync('bargains') || this.getMockBargains()
    
    // 根据当前标签筛选
    const filteredBargains = this.filterBargains(bargains, this.data.currentTab)
    
    this.setData({
      bargains: filteredBargains
    })
  },

  getMockBargains() {
    return [
      {
        id: 1,
        productId: 1,
        productTitle: '毕业趣旅行-高星好评-四日解锁京城',
        productImage: '',
        originalPrice: 400,
        targetPrice: 200,
        currentPrice: 320,
        progress: 20, // 完成百分比
        endTime: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000).toISOString(), // 2天后
        status: 'ongoing', // ongoing-进行中, success-已成功, failed-已失败
        helpCount: 15, // 已助力人数
        needHelpCount: 25 // 还需要助力人数
      },
      {
        id: 2,
        productId: 2,
        productTitle: '水舞间水上汇演门票',
        productImage: '',
        originalPrice: 367,
        targetPrice: 200,
        currentPrice: 200,
        progress: 100,
        endTime: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000).toISOString(),
        status: 'success',
        helpCount: 30,
        needHelpCount: 0,
        successTime: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000).toISOString()
      },
      {
        id: 3,
        productId: 3,
        productTitle: '北京故宫紫禁城门票',
        productImage: '',
        originalPrice: 1200,
        targetPrice: 600,
        currentPrice: 800,
        progress: 33,
        endTime: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000).toISOString(),
        status: 'failed',
        helpCount: 10,
        needHelpCount: 20
      }
    ]
  },

  filterBargains(bargains, tabIndex) {
    const statusMap = ['ongoing', 'success', 'failed']
    const status = statusMap[tabIndex]
    
    return bargains.filter(item => item.status === status).map(item => {
      // 格式化时间
      const endTime = new Date(item.endTime)
      const now = new Date()
      const diff = endTime - now
      
      let timeText = ''
      if (item.status === 'ongoing') {
        if (diff > 0) {
          const days = Math.floor(diff / (1000 * 60 * 60 * 24))
          const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
          timeText = days > 0 ? `${days}天${hours}小时` : `${hours}小时`
        } else {
          timeText = '已过期'
        }
      } else if (item.status === 'success') {
        timeText = this.formatTime(item.successTime)
      } else {
        timeText = this.formatTime(item.endTime)
      }
      
      return {
        ...item,
        timeText,
        formattedCurrentPrice: item.currentPrice.toFixed(2),
        formattedTargetPrice: item.targetPrice.toFixed(2),
        formattedOriginalPrice: item.originalPrice.toFixed(2)
      }
    })
  },

  formatTime(timeStr) {
    const date = new Date(timeStr)
    const month = date.getMonth() + 1
    const day = date.getDate()
    return `${month}月${day}日`
  },

  switchTab(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      currentTab: index
    })
    this.loadBargains()
  },

  goToProductDetail(e) {
    const productId = e.currentTarget.dataset.productId
    wx.navigateTo({
      url: `/pages/product-detail/product-detail?id=${productId}`
    })
  },

  goToBargainDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/bargain-detail/bargain-detail?id=${id}`
    })
  },

  shareBargain(e) {
    const id = e.currentTarget.dataset.id
    const bargain = this.data.bargains.find(b => b.id === id)
    
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    
    // 这里可以设置分享内容
    wx.showToast({
      title: '分享给好友助力',
      icon: 'none'
    })
  },

  goBack() {
    wx.navigateBack()
  }
})

